<?php

namespace Kollway\CodeGenerator\Generators;

class FormViewGenerator extends Generator
{
    public function generateFormViewString()
    {
        if ($this->form_type == 'dialog') {
//            $extends = "@extends('public.modal_base')";
        }
        $form_view_string = <<<FORM
@extends('public.base')

@section('main-content')
    <h2>@if(isset(\$data['id']))修改@else新增@endif{$this->form_title}</h2>

    <div class="row">
        <div class="col-md-12">
            <div class="panel panel-primary">
                <div class="panel-body">
                    <form action="{{build_action('Admin\\{$this->model_name}Controller@postForm')}}" method="post" role="form" class="form-horizontal form-groups-bordered">
                        <input type="hidden" name="id" value="{{\$data['id'] or 0}}">
                        <input type="hidden" name="sort_index" value="{{\$data['sort_index'] or 999}}">
                        <input type="hidden" name="create_time" value="{{\$data['create_time'] or time()}}">
                        <input type="hidden" name="update_time" value="{{\$data['update_time'] or time()}}">\n\n
FORM;

        // 表单输入框
        foreach ($this->column_info as $column) {
            if (!in_array($column['field'], ['id', 'status', 'sort_index', 'is_deleted', 'create_time', 'update_time'])) {
                if (in_array($column['field'], $this->passwords)) {
                    // 密码输入框
                    $form_view_string .= <<<LIST
                        <div class="form-group">
                            <label class="col-sm-3 control-label">{$column['comment']}</label>
                            <div class="col-sm-5">
                                <input type="password" class="form-control" name="{$column['field']}" autocomplete="off">
                            </div>
                        </div>\n\n
LIST;
                } elseif (isset($this->radios[$column['field']])) {
                    // 单选框, 选项来自 yaml 配置的 values 数组
                    $form_view_string .= <<<LIST
                        <div class="form-group">
                            <label class="col-sm-3 control-label">{$column['comment']}</label>
                            <div class="col-sm-5">\n
LIST;
                    foreach ($this->radios[$column['field']]['values'] as $key => $value) {
                        $v = $key + ($this->radios[$column['field']]['start_index'] ?? 0);
                        $form_view_string .= <<<LIST
                                <div class="col-md-2">
                                    <label class="radio"><input type="radio" name="{$column['field']}" value="{$v}" @if(array_get(\$data, '{$column['field']}') == $v) checked @endif>{$value}</label>
                                </div>\n
LIST;
                    }

                    $form_view_string .= <<<LIST
                            </div>
                        </div>\n\n
LIST;
                } elseif (isset($this->checkboxes[$column['field']])) {
                    $form_view_string .= <<<LIST
                        <div class="form-group">
                            <label class="col-sm-3 control-label">{$column['comment']}</label>
                            <div class="col-sm-offset-3 col-sm-5">\n
LIST;
                    foreach ($this->checkboxes[$column['field']]['values'] as $key => $value) {
                        $v = $key + ($this->checkboxes[$column['field']]['start_index'] ?? 0);
                        $form_view_string .= <<<LIST
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name="{$column['field']}" value="{$v}">{$value}
                                    </label>
                                </div>\n
LIST;
                    }
                    $form_view_string .= <<<LIST
                            </div>
                        </div>\n\n
LIST;
                } elseif (in_array($column['field'], $this->images)) {
                    $form_view_string .= <<<LIST
                        <div class="form-group">
                            <label class="col-sm-3 control-label">{$column['comment']}(请上传200x200尺寸大小的图片)</label>
                            <div class="col-sm-5">
                                <div class="uploadable">
                                    <input type="hidden" name="{$column['field']}" value="{{\$data['{$column['field']}'] or ''}}">
                                    <div class="thumbnail upload-img">
                                        <a class="fancybox" href="{{img_url_for_form(array_get(\$data, '{$column['field']}'))}}">
                                            <img width="200" height="200" src="{{img_url_for_form(array_get(\$data, '{$column['field']}'))}}" alt="...">
                                        </a>
                                    </div>
                                    <div>
                                        <span class="btn btn-white btn-file" onclick="$(this).next().click();">选择图片</span>
                                        <input type="file" name="..." accept="image/png,image/jpg,image/jpeg" class="hidden">
                                    </div>
                                </div>
                            </div>
                        </div>\n\n
LIST;
                } elseif (in_array($column['field'], $this->textareas)) {
                    $form_view_string .= <<<LIST
                        <div class="form-group">
                            <div class="col-sm-3 control-label"><p>{$column['comment']}</p></div>
                            <div class="col-sm-5">
                                <textarea id="{$column['field']}" name="{$column['field']}" class="form-control">{{\$data['{$column['field']}']}}</textarea>
                            </div>
                        </div>\n\n
LIST;
                } else {
                    $form_view_string .= <<<LIST
                        <div class="form-group">
                            <label for="name"  class="col-sm-3 control-label">{$column['comment']}</label>
                            <div class="col-sm-5">
                                <input type="text" class="form-control" name="{$column['field']}" value="{{\$data['{$column['field']}'] or ''}}">
                            </div>
                        </div>\n\n
LIST;
                }
            }
        }

        $form_view_string .= <<<FORM

                        <div class="form-group">
                            <div class="col-sm-offset-3 col-sm-5">
                                <button class="btn btn-primary submit-btn ajax-post" id="submit" type="submit" target-form="form-horizontal">确 定</button>
                                <button class="btn btn-return" onclick="history.back();return false;">返 回</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
@stop
FORM;
        $this->content = $form_view_string;

        return $form_view_string;
    }
}